import { AdManager } from "../ads/AdManager";
import { ANSWER_STATE_ENUM, BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { AudioManager1 } from "../manager/AudioManager1";
import { EventManager } from "../manager/EventManager";
import { GameManager } from "../manager/GameManager";
import { LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class OverUI extends cc.Component {
    @property(cc.Node)
    tipBtn: cc.Node = null;
    @property(cc.Node)
    addBtn: cc.Node = null;
    onLoad() {
        AudioManager1.stopAllAudio();
        cc.log("游戏结束");

        AudioManager1.playAudio("shibai", 1, BUNDLE_TYPE_ENUM.GAME)
        AdManager.stopVideoScreen();
        AdManager.showIntersAd();
        this.initAddBtn();
        EventManager.ins.on(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);
    }
    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);

    }

    /** 再玩一次 */
    clickBtnAginGame() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        let animTime = UIManager.ins.closeAll();
        setTimeout(() => LevelManager.ins.loadLevelPre(), animTime * 1000);

        AdManager.exitLv(LevelManager.ins.curLvInfo.name);

        // LevelManager.ins.loadLevelPre()
    }

    /** 跳过本关 */
    clickBtnPass() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        GameManager.ins.passLevel();
        AdManager.exitLv(LevelManager.ins.curLvInfo.name);
    }

    /** 返回主页 */
    clickBtnReturn() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        UIManager.ins.closeAll();
        UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
        AdManager.exitLv(LevelManager.ins.curLvInfo.name);
    }
    updateTipVideoIcon() {
        let spr = this.tipBtn.getChildByName("spr");
        let sprVideo = this.tipBtn.getChildByName("sprVideo");
        sprVideo.active = LevelManager.ins.getAnswerState() === ANSWER_STATE_ENUM.LOCKED;
        spr.active = !sprVideo.active;
    }
    initAddBtn() {
        this.addBtn.active = false;
        // AdManager.isShowAddShortcutBtn((active: boolean) => {
        //     this.addBtn.active = active;
        // });
    }
    hideAddBtn() {
        this.addBtn.active = false;
    }
    /** 答案 */
    clickBtnTip() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        let answerState = LevelManager.ins.getAnswerState();
        if (answerState === ANSWER_STATE_ENUM.LOCKED) {
            AdManager.showVideoAd(() => {
                LevelManager.ins.unlockLevelTip1();
                UIManager.ins.showPop("TipUI", BUNDLE_TYPE_ENUM.GAME, LevelManager.ins.curKeyInfo, false, true);
                this.updateTipVideoIcon();
            });
        } else {
            UIManager.ins.showPop("TipUI", BUNDLE_TYPE_ENUM.GAME, LevelManager.ins.curKeyInfo, false, true);
        }
    }

     /** 点击添加桌面 */
     clickBtnAddShortCut() {
        AudioManager1.playClick();
        // UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
        AdManager.addTable(() => EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN));
    }
}
